home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 February: Tool Chest / Dev.CD Feb 99 TC.toast / Tool Chest / Interapplication Communication / ScriptableStuffItEngine / Project / ScriptableStuffItEngine.cp < prev    next >
Encoding:
Text File  |  1998-12-02  |  3.2 KB  |  182 lines  |  [TEXT/CWIE]

  1. /*
  2.     You may incorporate this Apple sample source code into your program(s) without
  3.     restriction. This Apple sample source code has been provided "AS IS" and the
  4.     responsibility for its operation is yours. You are not permitted to redistribute
  5.     this Apple sample source code as "Apple sample source code" after having made
  6.     changes. If you're going to re-distribute the source, we require that you make
  7.     it clear in the source that the code was descended from Apple sample source
  8.     code, but that you've made changes.
  9. */
  10.  
  11. #include "ScriptableStuffItEngine.h"    // app
  12. #include "FullPath.h"                    // MoreFiles
  13.  
  14. #ifndef __LOWMEM__
  15. #    include <LowMem.h>
  16. #endif
  17.  
  18. #ifndef __APPLEEVENTS__
  19. #    include <AppleEvents.h>
  20. #endif
  21.  
  22. #ifndef __SOUND__
  23. #    include <Sound.h>
  24. #endif
  25.  
  26. Boolean gQuitting;
  27.  
  28. static pascal OSErr SetUpMenuBar (void)
  29. {
  30.     OSErr err = noErr;
  31.  
  32.     Handle menuBar = GetNewMBar (128);
  33.  
  34.     if (!menuBar)
  35.         err = nilHandleErr;
  36.     else
  37.     {
  38.         SetMenuBar (menuBar);
  39.         DisposeHandle (menuBar);
  40.         InvalMenuBar ( );
  41.     }
  42.  
  43.     return err;
  44. }
  45.  
  46. static pascal OSErr ProcessFileMenuCommand (short command)
  47. {
  48.     OSErr err = noErr;
  49.  
  50.     if (command == 1)
  51.         gQuitting = true;
  52.  
  53.     return err;
  54. }
  55.  
  56. static pascal OSErr ProcessMenuCommand (long command)
  57. {
  58.     OSErr err = noErr;
  59.  
  60.     switch (command >> 16)
  61.     {
  62.         case 128 : // apple menu
  63.             break;
  64.         case 129 : // file menu
  65.             err = ProcessFileMenuCommand (command);
  66.             break;
  67.     }
  68.  
  69.     return err;
  70. }
  71.  
  72. static pascal OSErr ProcessKeyDown (const EventRecord &event)
  73. {
  74.     OSErr err = noErr;
  75.  
  76.     if (event.modifiers & cmdKey)
  77.         err = ProcessMenuCommand (MenuKey (event.message & charCodeMask));
  78.  
  79.     return err;
  80. }
  81.  
  82. static pascal OSErr ProcessMouseDown (const EventRecord &event)
  83. {
  84.     OSErr err = noErr;
  85.  
  86.     WindowRef whichWindow;
  87.  
  88.     short findWindowPartCode = FindWindow (event.where, &whichWindow);
  89.  
  90.     switch (findWindowPartCode)
  91.     {
  92.         case inMenuBar :
  93.  
  94.             err = ProcessMenuCommand (MenuSelect (event.where));
  95.             HiliteMenu (0);
  96.             break;
  97.     }
  98.  
  99.     return err;
  100. }
  101.  
  102. static pascal OSErr InitMac (void)
  103. {
  104.     OSErr err = noErr;
  105.  
  106.     MaxApplZone ( );
  107.     InitGraf (&(qd.thePort));
  108.     InitFonts ( );
  109.     InitWindows ( );
  110.     InitMenus ( );
  111.     TEInit ( );
  112.     InitDialogs (nil);
  113.  
  114.     return err;
  115. }
  116.  
  117. static pascal OSErr FullPathToFSSpec
  118.     (DescType givenType, const void *data, Size size, DescType desiredType, long, AEDesc *result)
  119. {
  120.     OSErr err = noErr;
  121.  
  122.     if (givenType == typeChar && desiredType == typeFSS)
  123.     {
  124.         FSSpec fss;
  125.  
  126.         if (!(err = FSpLocationFromFullPath (size,data,&fss)))
  127.         {
  128.             err = AECreateDesc (typeFSS, &fss, sizeof (fss), result);
  129.         }
  130.     }
  131.  
  132.     return err;
  133. }
  134.  
  135. void main (void)
  136. {
  137.     OSErr err = noErr;
  138.  
  139.     if (!(err = InitMac ( )))
  140.     if (!(err = SetUpMenuBar ( )))
  141.     if (!(err = AEInstallEventHandler (typeWildCard,typeWildCard,ScriptableStuffItAppleEventHandler,0,false)))
  142.     if (!(err = AEInstallCoercionHandler (typeChar, typeFSS, AECoercionHandlerUPP (FullPathToFSSpec), 0, false, false)))
  143.     {
  144.         while (!gQuitting)
  145.         {
  146.             EventRecord event;
  147.  
  148.             WaitNextEvent (everyEvent,&event,-1,nil);
  149.  
  150.             switch (event.what)
  151.             {
  152.                 case keyDown :
  153.  
  154.                     err = ProcessKeyDown (event);
  155.                     break;
  156.  
  157.                 case kHighLevelEvent :
  158.  
  159.                     err = AEProcessAppleEvent (&event);
  160.                     break;
  161.  
  162.                 case mouseDown :
  163.  
  164.                     err = ProcessMouseDown (event);
  165.                     break;
  166.             }
  167.  
  168.             if (err)
  169.             {
  170.                 SysBeep (-1);
  171.                 err = noErr;
  172.             }
  173.         }
  174.     }
  175.  
  176.     if (err)
  177.     {
  178.         SysBeep (-1);
  179.         err = noErr;
  180.     }
  181. }
  182.